package com.wdb007.venus.svc.biz.account;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.dal.user.dao.UserPointDAO;
import com.wdb007.venus.svc.dal.user.dao.UserPointLogDAO;
import com.wdb007.venus.svc.dal.user.model.UserPointDO;
import com.wdb007.venus.svc.dal.user.model.UserPointLogDO;
import com.wdb007.venus.svc.facade.UserPointService;
import com.wdb007.venus.svc.facade.account.model.UserPointDTO;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("userPointService")
public class UserPointServiceImpl implements UserPointService {

	private Logger logger = LoggerFactory.getLogger(UserPointServiceImpl.class);
	
	@Autowired
	private UserPointDAO userPointDAO;
	
	@Autowired
	private UserPointLogDAO userPointLogDAO;
	
	@Override
	public int updateUserPoint(UserPointDTO userPointDTO) throws BizException {
		logger.info("保存用户积分信息: {}", JSON.toJSONString(userPointDTO));
		
		UserPointDO userPoint = userPointDAO.getByUserId(userPointDTO.getUserId());
		if(null != userPoint) {
			UserPointLogDO uplQuery = new UserPointLogDO();
			uplQuery.setPointId(userPoint.getId());
			uplQuery.setChangeType(userPointDTO.getChangeType());
			//49--表示押金增加积分
			if(49 == userPointDTO.getChangeType()) {
				int count = userPointLogDAO.countByCondition(uplQuery);
				if(count != 0) {
					return 0 ;
				}
			}else if(47 == userPointDTO.getChangeType()) { // 47--表示余额充值
				
			}
			userPoint.setPointValue(userPointDTO.getChangeValue() + userPoint.getPointValue());
			userPoint.setModifyTime(new Date());
			userPointDAO.update(userPoint);
			
			UserPointLogDO uplDO = new UserPointLogDO();
			uplDO.setChangeValue(userPointDTO.getChangeValue());
			uplDO.setChangeType(userPointDTO.getChangeType());
			uplDO.setCreateTime(new Date());
			uplDO.setPointId(userPoint.getId());
			userPointLogDAO.save(uplDO);
		}
		
		
		
		return 1;
	}

}
